package com.shareyi.simpleserver.core;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import com.shareyi.fileutil.FileUtil;
import com.shareyi.simpleserver.exception.SimpleServerException;

/**
 * This read the server Configuration file and store the server parameters.
 *@author David zhang
 *@since Tue 20 May 2011
 *@version 1.0
 */


public class ServerConfig {
	 
	private static final String CUR_DIR="curDir:";
	 
	 
	 private  int port = 8088;
	 private  String basePath = "D:/webserver";
	 private  String woxConfigPath;
	 private String webConfigPath;
	 private String bindPath;
	 private  int maxConn=100;
	 private  int access=1;
	 private boolean manyInstAllow=false;
	 private boolean postfixFilter;
	 private String[] postfixs;
	 private String index;
	public int getPort() {
		return port;
	}

	public String getBasePath() {
		return basePath;
	}
	
	public String getWoxConfigPath() {
		return woxConfigPath;
	}
	
	public int getMaxConn() {
		return maxConn;
	}
	
	public int getAccess() {
		return access;
	}
	
	public boolean isPostfixFilter() {
		return postfixFilter;
	}

	public String[] getPostfixs() {
		return postfixs;
	}
	
	public void setPort(int port) {
		this.port = port;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public void setWoxConfigPath(String woxConfigPath) {
		this.woxConfigPath = woxConfigPath;
	}

	public void setMaxConn(int maxConn) {
		this.maxConn = maxConn;
	}

	public void setAccess(int access) {
		this.access = access;
	}

	public void setPostfixFilter(boolean postfixFilter) {
		this.postfixFilter = postfixFilter;
	}

	public void setPostfixs(String[] postfixs) {
		this.postfixs = postfixs;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}
	
	public void setWebConfigPath(String webConfigPath) {
		this.webConfigPath = webConfigPath;
	}

	public boolean isManyInstAllow() {
		return manyInstAllow;
	}

	public void setManyInstAllow(boolean manyInstAllow) {
		this.manyInstAllow = manyInstAllow;
	}

	public static ServerConfig readServerConfig(File file) throws SimpleServerException
	 {
		ServerConfig config=new ServerConfig();
		
		Properties properties=new Properties();
		if(!file.exists()||!file.isFile())
			throw new SimpleServerException("server配置文件不存在");
		FileInputStream stream;
		try {
			stream = new FileInputStream(file);
			properties.load(stream);
			String baseDir=properties.getProperty("baseDir");
			String wcp=properties.getProperty("wox-config-path");
			config.setBasePath(getPath(baseDir));
			config.setWoxConfigPath(getPath(wcp));

			String webConfigPath=properties.getProperty("web-config-path");
			config.setWebConfigPath(getPath(webConfigPath));
			
			if(config.getBasePath()==null)
				throw new SimpleServerException("基本目录[baseDir]不能为空!");
			if(config.getWoxConfigPath()==null)
				throw new SimpleServerException("wox框架[wox-config-path]路径不能为空!");
			
			String postFilter=properties.getProperty("postfixFilter");
			boolean b=Boolean.valueOf(postFilter);
			config.setPostfixFilter(b);
			
			String postfixs=properties.getProperty("postfixs");
			if(postfixs==null&&b)
				throw new SimpleServerException("可接受类型[postfixs]不能为空!");
			if(b)
			config.setPostfixs(postfixs.split(","));
			config.setPort(getIntValue(properties, "port",8088));
			config.setMaxConn(getIntValue(properties,"maxConn",100));
			config.setAccess(getIntValue(properties,"access",1));
			config.setManyInstAllow(Boolean.valueOf(properties.getProperty("manyInstAllow")));
			config.setIndex(properties.getProperty("index"));
			String bindPath=properties.getProperty("bindPath");
			config.setBindPath(getPath(bindPath));
		} catch (Exception e) {
			throw new SimpleServerException("读取server配置文件出错");
		}
		
		return config;
	 }
	
	 private static String getPath(String path)
	 {
		 String parsePath=null;
		 if(path!=null)
			{
				if(path.startsWith(CUR_DIR))
				{
					parsePath=path.substring(path.indexOf(":")+1).trim();
					if(parsePath.startsWith("/")||parsePath.startsWith("\\"))
						parsePath=FileUtil.getRunPath()+parsePath;
					else parsePath=FileUtil.getRunPath()+"/"+parsePath;
				}else
					parsePath=path;
			}
		 return parsePath;
	 }
	 
	 private static int getIntValue(Properties properties,String key,int defVal)
	 {
		 try{
			 return Integer.parseInt(properties.getProperty(key));
		 }catch(Exception e)
		 {}
		return defVal; 
	 }

	public void setBindPath(String bindPath) {
		this.bindPath = bindPath;
	}

	public String getBindPath() {
		return bindPath;
	}

	public String getWebConfigPath() {
		return this.webConfigPath;
	}
	 
}
